﻿namespace zUnitTesting.TestScripts
{
    using Engine.Managers;
    using Microsoft.Practices.ServiceLocation;
    using NUnit.Framework;
    using NUnit.Mocks;
    using System.Linq;
    using TransferObjects;
    using zUnitTesting.TestTools;

    [TestFixture]
    public class EngineTesting
    {
        private DynamicMock iWebClientManagerMock;

        [SetUp]
        public void Init()
        {
            iWebClientManagerMock = new DynamicMock(typeof(IWebClientAdapter));
        }

        [TestCase("show tables", "http://query.yahooapis.com/v1/public/yql?q=show+tables&format=json&env=http%3a%2f%2fdatatables.org%2falltables.env")]
        public void TestYahooResponse(string resquest, string expectedQueryString)
        {
            string request = "show tables";

            IYahooServiceRequestor service = ServiceLocator.Current.GetInstance<IYahooServiceRequestor>();

            // configuring the Mock for IWebClientAdapter
            iWebClientManagerMock.ExpectAndReturn("DownloadString", FileLoader.RetrieveTestFileContent("tableList.json", "TestFiles"), expectedQueryString);

            // Using created Mock in YahooServiceRequestor
            ((YahooServiceRequestor)service).WebClientManager = (IWebClientAdapter)iWebClientManagerMock.MockInstance;

            YahooResponse response = service.Execute(request);

            Assert.AreEqual(expectedQueryString, response.QueryString);
            Assert.IsNotNull(response.Query.Results.Tables.Where(t => t.Content == "yahoo.finance" && t.Src == null && t.Security == null));
            Assert.IsNotNull(response.Query.Results.Tables.Where(t => t.Content == "yahoo.queries" && t.Src == "uri" && t.Security == null));
            Assert.IsNotNull(response.Query.Results.Tables.Where(t => t.Content == "yahoo.queries.query" && t.Src == null && t.Security == "USER"));
        }
    }
}
